<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 2017/10/31
 * Time: 15:42
 */

namespace app\leader\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use app\customer\validate\Level;
use app\manager\model\Manager;
use think\Db;
use app\common\log\addRole\ManagerAdd;

class Index extends Admin
{
    private $a_data;

    public function leaderList()
    {
        $a_where = [
            'type' => 1,
        ];
        $a_where = array_merge($a_where, $this->getMap());
        $a_dataList = Db::name('manager')->where($a_where)->order('id desc')->paginate();

        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns(config('manager_list'))
            ->setSearch([
                'department' => '部门',
                'name' => '姓名',
                'number' => '柜员号',
            ])
            ->addRightButton('edit')
            ->setTableName('manager')
            ->setRowList($a_dataList)
            ->fetch();
    }

    function edit($id = '')
    {
        if (request()->isAjax()):
            $this->a_data = input('');
            //验证数据
            $o_validate = validate('Manager');
            if ($o_validate->scene('update')->check($this->a_data) === false):
                $this->error($o_validate->getError());
            endif;
            //修改数据
            $o_model = new Manager();
            if ($o_model->isUpdate()->save($this->a_data) === false):
                //记录失败日志
                $o_log = new ManagerAdd([
                    'role_id' => $id,
                    'status' => 0,
                    'update' => true,
                    'error' => '',
                    'role_type' => 3,
                    'content' => json_encode($this->a_data)
                ]);
                $o_log->addOne();
                $this->error("操作失败！");
            else:
                $o_log = new ManagerAdd([
                    'role_id' => $id,
                    'status' => 1,
                    'update' => true,
                    'error' => '',
                    'role_type' => 3,
                    'content' => json_encode($this->a_data)
                ]);
                $o_log->addOne();
                $this->success("更新成功！");
            endif;
        endif;
        $a_items = config('manager_info');
        $a_items[3][0] = 'static';
        $a_items[3][3] = '不可修改';
        return ZBuilder::make('form')
            ->addHidden('id', $id)
            ->addFormItems($a_items)
            ->setFormData(Db::name('manager')->where('id', $id)->find())
            ->fetch();
    }
}